package com.leo.blog.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.leo.blog.entity.TbFilestr;
import com.leo.blog.service.TbFilestrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.leo.blog.annotatiton.SystemLog;
import com.leo.blog.dao.BlogDao;
import com.leo.blog.dto.BlogExecution;
import com.leo.blog.dto.ImageHolder;
import com.leo.blog.entity.Blog;
import com.leo.blog.enums.BlogStateEnum;
import com.leo.blog.exceptions.BlogOperationException;
import com.leo.blog.service.BlogService;
import com.leo.blog.util.ConstantUtil;
import com.leo.blog.util.ImageUtil;
import com.leo.blog.util.PageCalculator;
import com.leo.blog.util.PathUtil;

@Service
public class BlogServiceImpl implements BlogService{
	@Autowired
	private BlogDao blogDao;

	@Override
	@Transactional
	@SystemLog(description = ConstantUtil.BLOG_ADD, userType = ConstantUtil.USERTYPE_ADMIN)
	public BlogExecution addBlog(String name, Blog blog) throws BlogOperationException {
		//空值判断
		if(blog == null) {
			//文章信息为空
			return new BlogExecution(BlogStateEnum.NULL_BLOG);
		}
		try {
			//给文章信息添加初始值
			blog.setClickNum(0);
			blog.setCommentNum(0);
			blog.setAgreeNum(0);
			blog.setIsTop(-1);//-1:非置顶  1:置顶
			blog.setIsRecommend(-1);//-1:不推荐, 1:推荐
			//blog.setStatus(1);//-1:草稿, 1:正文, 2:回收站
			blog.setCreateTime(new Date());
			blog.setUpdateTime(new Date());
			
			//添加博客信息
			int effectedNum = blogDao.insertBlog(blog);
			//判断插入是否成功
			if(effectedNum <= 0) {
				throw new BlogOperationException("文章发表失败");
			}
		}catch (Exception e) {
			throw new BlogOperationException("addBlog error:" + e.getMessage());
		}
		return new BlogExecution(BlogStateEnum.SUCCESS, blog);
	}

	@Override
	public List<Blog> getBlogCountByStatus() {
		return blogDao.queryBlogCountByStatus();
	}

	@Autowired
	private TbFilestrService tbFilestrService;

	@Override
	@SystemLog(description = ConstantUtil.UPLOAD_IMAGES, userType = ConstantUtil.USERTYPE_ADMIN)
	public String uploadBlogImages(String name, int blogerId, ImageHolder thumbnail) {
		String fileNames = "";
		//判断图片是否为空
		if(thumbnail.getImage() != null) {
			//图片操作
			try {
				//添加图片路径
				String path = addBlogImgs(thumbnail);
				return path;
			} catch (Exception e) {
				throw new BlogOperationException("addBlogImgs error:" + e.getMessage());
			}
		}
		return fileNames;
	}

	private String addBlogImgs( ImageHolder thumbnail) {
		//获取blog图片目录的相对值路径
		String str = ImageUtil.generateThumbnail(thumbnail);

		return str;
	}
	
	

	@Override
	@SystemLog(description = ConstantUtil.BLOG_UPDATE, userType = ConstantUtil.USERTYPE_ADMIN)
	public BlogExecution modifyBlog(String name, Blog blog) throws BlogOperationException {
		if(blog == null) {
			//文章不存在
			return new BlogExecution(BlogStateEnum.NULL_BLOG);
		}else {
			try {
				//更新文章
				blog.setUpdateTime(new Date());
				int effectedNum = blogDao.updateBlog(blog);
				if(effectedNum <= 0) {
					return new BlogExecution(BlogStateEnum.INNER_ERROR);
				}else {
					//更新成功,返回最新的信息
					blog = blogDao.queryByBlogId(blog.getBlogId());
					return new BlogExecution(BlogStateEnum.SUCCESS, blog);
				}
			} catch (Exception e) {
				throw new BlogOperationException("modifyBlog error: " + e.getMessage());
			}	
		}
	}
	
	@Override
	@SystemLog(description = ConstantUtil.BLOG_UPDATE, userType = ConstantUtil.USERTYPE_USER)
	public BlogExecution modifyBlogClickNum(String name, Blog blog) throws BlogOperationException {
		if(blog == null) {
			//文章不存在
			return new BlogExecution(BlogStateEnum.NULL_BLOG);
		}else {
			try {
				//更新文章
				blog.setUpdateTime(new Date());
				int effectedNum = blogDao.updateBlog(blog);
				if(effectedNum <= 0) {
					return new BlogExecution(BlogStateEnum.INNER_ERROR);
				}else {
					//更新成功,返回最新的信息
					blog = blogDao.queryByBlogId(blog.getBlogId());
					return new BlogExecution(BlogStateEnum.SUCCESS, blog);
				}
			} catch (Exception e) {
				throw new BlogOperationException("modifyBlog error: " + e.getMessage());
			}	
		}
	}

	@Override
	@SystemLog(description = ConstantUtil.BLOG_SELECT,userType=ConstantUtil.USERTYPE_USER) 
	public Blog getByBlogId(int blogId) {
		//根据id查询信息
		return blogDao.queryByBlogId(blogId);
	}
	@Override
	public Blog getBlogByIdPreAndNext(int blogId) {
		// 根据id查询前后文章
		return blogDao.queryByBlogIdPreAndNext(blogId);
	}

	@Override
	public BlogExecution getBlogList(Blog blogCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,调用dao层返回相关的文章列表
		List<Blog> blogList = blogDao.queryBlogList(blogCondition, rowIndex, pageSize);
		//根据相同的查询条件,返回文章总数
		int count = blogDao.queryBlogCount(blogCondition);
		BlogExecution be = new BlogExecution();
		if(blogList != null) {
			be.setBlogList(blogList);
			be.setCount(count);
		}else {
			be.setState(BlogStateEnum.INNER_ERROR.getState());
		}
		return be;
	}
	
	@Override
	public BlogExecution getBlogListClick(Blog blogCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//根据查询条件,调用dao层返回相关的文章列表
		List<Blog> blogList = blogDao.queryBlogListByClick(blogCondition, rowIndex, pageSize);
		//根据相同的查询条件,返回文章总数
		int count = blogDao.queryBlogCount(blogCondition);
		BlogExecution be = new BlogExecution();
			if(blogList != null) {
				be.setBlogList(blogList);
				be.setCount(count);
			}else {
				be.setState(BlogStateEnum.INNER_ERROR.getState());
			}
			return be;
	}

	@Override
	public Map<String, List<Blog>> getBlogByAllType() {
		Map<String, List<Blog>> modelMap = new HashMap<>();
		List<Blog> listBlog = new ArrayList<>();
		listBlog = blogDao.queryBlogByAllType();
		if(listBlog.size() > 0) {
			for(Blog blog : listBlog) {
				if(modelMap.containsKey(blog.getBlogType().getBlogTypeName())) {
					modelMap.get(blog.getBlogType().getBlogTypeName()).add(blog);
				}else {
					List<Blog> blogList = new ArrayList<>();
					blogList.add(blog);
					modelMap.put(blog.getBlogType().getBlogTypeName(), blogList);
				}
			}
		}
		return modelMap;
	}

	@Override
	public int getBlogByMaxId() {
		return blogDao.queryByBlogMaxId();
	}

	@Override
	public BlogExecution getBlogListByDate(Blog blogCondition) {
		BlogExecution be = new BlogExecution();
		if(blogCondition != null) {
			List<?> blogList2 = blogDao.queryBlogListByDate(blogCondition);
			if(blogList2.size() >= 0) {
				be.setBlogList2(blogList2);
				be.setState(BlogStateEnum.SUCCESS.getState());
			}else {
				be.setState(BlogStateEnum.OFFLINE.getState());
			}
		}else {
			be.setState(BlogStateEnum.NULL_BLOG.getState());
		}
		return be;
	}

	@Override
	public BlogExecution getBlogByClick() {
		BlogExecution be = new BlogExecution();
		List<?> blogList2 = blogDao.queryBlogByClick();
		if(blogList2.size() >= 0) {
			be.setBlogList2(blogList2);
			be.setState(BlogStateEnum.SUCCESS.getState());
		}else {
			be.setState(BlogStateEnum.OFFLINE.getState());
		}
		return be;
	}

	@Override
	public BlogExecution getBlogListByStatus() {
		List<?> blogList2 = blogDao.queryBlogListByStatus();
		BlogExecution be = new BlogExecution();
		if(blogList2.size() > -1) {
			be.setBlogList2(blogList2);
			be.setState(BlogStateEnum.SUCCESS.getState());
		}else {
			be.setState(BlogStateEnum.OFFLINE.getState());
		}
		return be;
	}

	




	
	
	
	
	
	
	
	
	
	
	

}
